import numStatus from "./index"
import deepClone from "@/utils/deepClone"
// 创建数据仓库
let reducer = (state = { ...numStatus.state }, action: { type: string, val: number }) => {
    let newState = deepClone(state)
    // 判断
    // 如何避免switch，将action的type和case的值进行对比，类似遍历，case后面的值提为对象actionNames
    // switch (action.type) {
    //     case numStatus.add1:
    //         numStatus.actions['add1'](newState, action)
    //         break;
    //     case numStatus.test:
    //         numStatus.actions['test'](newState, action)
    //         break
    //     default:
    //         console.log(123);
    // }
    /* 优化，避免重复写case
        action.type与actionNames的每一项对比
        直接在index配置即可
    */
    for (let key in numStatus.actionsNames) {
        //判断是否相等
        if (action.type === numStatus.actionsNames[key]) {
            numStatus.actions[numStatus.actionsNames[key]](newState, action)
        }
    }

    return newState
}

export default reducer